package com.guodi.model.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guodi.common.utils.SortListUtil;
import com.guodi.common.utils.UuidUtil;
import com.guodi.model.entity.DiagnosisProblemScene;
import com.guodi.model.entity.IdtFillProblem;
import com.guodi.model.entity.UeProjectManger;
import com.guodi.model.entity.UeUpdateRecords;
import com.guodi.model.mapper.DiagnosisProblemSceneMapper;
import com.guodi.model.mapper.UeProjectMangerMapper;
import com.guodi.model.service.ICacheService;
import com.guodi.model.service.IIdtFillProblemService;
import com.guodi.model.service.IUeProjectMangerService;
import com.guodi.model.service.IUeUpdateRecordsService;
import com.guodi.model.vo.UeProjectMangerVo;
import io.swagger.annotations.ApiImplicitParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

/**
 * @描述：更新评估-项目管理表 服务类
 * @作者: 徐金洲
 * @日期: 2023-05-05
 */
@Service
public class UeProjectMangerServiceImpl extends ServiceImpl<UeProjectMangerMapper, UeProjectManger> implements IUeProjectMangerService {

    @Resource
    public IUeUpdateRecordsService ueUpdateRecordsService;

    @Resource
    private UeProjectMangerMapper ueProjectMangerMapper;

    @Resource
    private ICacheService cacheService;

    @Resource
    public IIdtFillProblemService idtFillProblemService;

    @Override
    public Page listByEntity(Page page, UeProjectManger ueProjectManger) {
        LambdaQueryWrapper<UeProjectManger> queryWrapper = (new QueryWrapper()).lambda();
        //项目名称
        if (org.apache.commons.lang.StringUtils.isNotBlank(ueProjectManger.getProjectName())) {
            queryWrapper.like(UeProjectManger::getProjectName, ueProjectManger.getProjectName());
        }
        //行政区划
        if (org.apache.commons.lang.StringUtils.isNotBlank(ueProjectManger.getDivisionCode())) {
            queryWrapper.like(UeProjectManger::getDivisionCode, ueProjectManger.getDivisionCode());
        }
        //改善指标
        if (org.apache.commons.lang.StringUtils.isNotBlank(ueProjectManger.getImproveIdt())) {
            queryWrapper.eq(UeProjectManger::getImproveIdt, ueProjectManger.getImproveIdt());
        }
        //项目进度
        if (org.apache.commons.lang.StringUtils.isNotBlank(ueProjectManger.getProjectSchedule())) {
            queryWrapper.eq(UeProjectManger::getProjectSchedule, ueProjectManger.getProjectSchedule());
        }
        queryWrapper.orderByDesc(UeProjectManger::getCreateTime);
        page = ueProjectMangerMapper.selectPage(page, queryWrapper);
        List<UeProjectManger> list = page.getRecords();
        page.setRecords(toVo(list));
        return page;
    }


    /**
     * @作者: 徐金洲 2023/05/08
     * @描述: 转VO, 转字典
     */
    private List<UeProjectMangerVo> toVo(List<UeProjectManger> list) {
        List<UeProjectMangerVo> listVo = new ArrayList<>();
        for (UeProjectManger row : list) {
            UeProjectMangerVo vo = new UeProjectMangerVo();
            BeanUtils.copyProperties(row, vo);
            vo.setProjectScheduleName(cacheService.getDictValue("xmjd", vo.getProjectSchedule()));

            //问题名称
            String respondProblem = idtFillProblemService.getProblemNameById(vo.getRespondProblem());
            if(StringUtils.isNotBlank(respondProblem)){
                vo.setRespondProblem(respondProblem);
            }
            listVo.add(vo);
        }
        return listVo;
    }


    @Override
    public boolean update(UeProjectManger ueProjectManger,String processedBy) {
        //查询当前的项目进度和保存的项目进度是否相同,不相同就保存更新记录数据
        UeProjectManger um = ueProjectMangerMapper.selectById(ueProjectManger.getId());
        //先判断当前的项目面积(项目面积是空就是没有导入地图范围)和项目进度是否更改
        if(!ueProjectManger.getArea().equals(um.getArea())){
            //保存更新记录数据
            UeUpdateRecords ueUpdateRecords = new UeUpdateRecords();
            ueUpdateRecords.setId(UuidUtil.get32UUID());
            ueUpdateRecords.setProjectId(ueProjectManger.getId());
            ueUpdateRecords.setProcessedBy(processedBy);
            ueUpdateRecords.setUpdateContent("导入地图范围");
            ueUpdateRecords.setUpdateTime(new Date());
            ueUpdateRecordsService.save(ueUpdateRecords);
        }
        if(!ueProjectManger.getProjectSchedule().equals(um.getProjectSchedule())){
            //保存更新记录数据
            UeUpdateRecords ueUpdateRecords = new UeUpdateRecords();
            ueUpdateRecords.setId(UuidUtil.get32UUID());
            ueUpdateRecords.setProjectId(ueProjectManger.getId());
            ueUpdateRecords.setProcessedBy(processedBy);
            String projectScheduleName = cacheService.getDictValue("xmjd", ueProjectManger.getProjectSchedule());
            ueUpdateRecords.setUpdateContent("项目进度修改为: " + projectScheduleName);
            ueUpdateRecords.setUpdateTime(new Date());
            ueUpdateRecordsService.save(ueUpdateRecords);
        }
        //修改项目管理信息
        boolean temp = this.saveOrUpdate(ueProjectManger);
        return temp;
    }

    @Override
    public List<UeProjectMangerVo> listByProblemId(List<String> problemIds) {

        List<UeProjectManger> results = new ArrayList<>();
        for(String problemId : problemIds){
            LambdaQueryWrapper<UeProjectManger> wrapper = new LambdaQueryWrapper<>();
            wrapper.like(UeProjectManger::getRespondProblem,problemId);
            wrapper.orderByAsc(UeProjectManger::getCreateTime);
            List<UeProjectManger> list = this.list(wrapper);
            results.addAll(list);
        }
        if(CollectionUtils.isNotEmpty(results)){
            // 排序
            SortListUtil.sort(results,"getYear","asc");
        }

        return toVo(results);
    }
}
